
bt.module({builder: function(bt) {
    var j$ = jQuery;

    var _addOrRemoveFriend = function(url, data) {
        j$(".friend-status").css('opacity', 0.3);
        var uid = j$("input.user-id").val();
        j$.ajax({
            url: url,
            data: data,
            type: "GET",
            success: function(html){
                j$(location).attr("href", "/u/" + uid);
            }
        });
    };

    j$(document).ready(function() {
        j$(".not-friend").click(function() {
            var popup = $("#friend-request-popup");
            var overlay = $("#overlay");
            popup.fadeIn();
            overlay.show();
            var windowWidth = document.documentElement.clientWidth;
            var windowHeight = document.documentElement.clientHeight;
            var popupHeight = popup.height();
            var popupWidth = popup.width();
            popup.css({
                "position": "fixed",
                "top": windowHeight / 2 - popupHeight / 2,
                "left": windowWidth / 2 - popupWidth / 2
            });
            overlay.click(function() {
                popup.fadeOut();
                overlay.hide();
            });
            $("#friend-request-popup .close").click(function() {
                popup.fadeOut();
                overlay.hide();
            });
        });

        j$("form#friend").submit(function() {
            var url = '/users/add_friend/';
            var data = j$("form#friend").serialize();
            if (j$("form#friend textarea").val().length > 50){
                alert("好友请求最多只能有50字！");
                return false;
            }
            _addOrRemoveFriend(url, data);
            return false;
        });
    });
}});
